//
//  huiyuanDetailsViewController.m
//  daogouApp
//
//  Created by panquanReabam.com on 15-6-2.
//  Copyright (c) 2015年 jiezsoft.com. All rights reserved.
//

#import "MCouponAViewController.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "HAODEsingleton.h"
#import "ProductView.h"
#import "UIViewExt.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "orderListCell.h"

#import "OrderDetailViewController.H"
#import "ServiceDetailViewController.H"
#import "RefundDetailViewController.H"
#import "BusinessMemberfindIntegralListViewController.h"
#import "BusinessMemberAddressViewController.h"
#import "BusinessCouponDetailViewController.h"
#import "BusinessActDetailViewController.h"
@interface MCouponAViewController ()<MBProgressHUDDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>

@property (strong, nonatomic) UIScrollView * scrollView;
@end

@implementation MCouponAViewController


{   UIView*beizhuView;
    UILabel *line;
//    UIScrollView*myscrollView;
    HAODEsingleton* commdity;
//    UIImageView* imageview;
//    UILabel *lineLabel1;

    NSMutableArray*isUsed;
    NSMutableArray*endDate;
    NSMutableArray*couponId;
    NSMutableArray*beginDate;
    NSMutableArray*useLimitAmt;
    NSMutableArray*price;
    NSMutableArray*useTime;
    UITableView *table_list1;
    
}
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
 
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    

    
    commdity=[HAODEsingleton shared];
    table_list1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-44) style:UITableViewStylePlain];
    table_list1.delegate = self;
    table_list1.dataSource = self;
    table_list1.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table_list1];

       [self liebiaoshuju];  
   }




-(void)liebiaoshuju{
    
    
    
    
   


    
    
 
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* Token1 = [user1 objectForKey:@"tokenId"];
    
    NSMutableDictionary *param1 = [NSMutableDictionary new];
    [param1 setValue:Token1 forKey:@"tokenId"];
    [param1 setValue:commdity.memberId forKey:@"memberId"];
//  [param1 setValue:@"1" forKey:@"searchType"];
    
    NSLog(@"====%@",param1);
    
    [HttpRequest requestData:@"Business/MCoupon/List" params:param1 success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                
                
                if (arrr.count>0) {
                    
                    
                    isUsed= [[NSMutableArray alloc] init];
                    endDate= [[NSMutableArray alloc] init];
                    couponId= [[NSMutableArray alloc] init];
                    beginDate= [[NSMutableArray alloc] init];
                    useLimitAmt= [[NSMutableArray alloc] init];
                    price= [[NSMutableArray alloc] init];
                    useTime= [[NSMutableArray alloc] init];
                    
                    for (NSDictionary* dicc in arrr )
                    {
                        [isUsed addObject:[dicc valueForKey:@"isUsed"]];
                        [endDate addObject:[dicc valueForKey:@"endDate"]];
                        [couponId addObject:[dicc valueForKey:@"couponId"]];
                        [beginDate addObject:[dicc valueForKey:@"beginDate"]];
                        [useLimitAmt addObject:[dicc valueForKey:@"useLimitAmt"]];
                        [price addObject:[dicc valueForKey:@"price"]];
                        
                        [useTime addObject:[dicc valueForKey:@"useTime"]];
                        
                    }
                    
                    
                }
                [table_list1 reloadData];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                // [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
return [couponId count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    orderListCell* cell = (orderListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"orderListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    /////////////////
    if (tableView == table_list1) {
        
        if ([[NSString stringWithFormat:@"%@", [isUsed objectAtIndex:indexPath.row]]isEqualToString:@"1"]) {
            cell = [[orderListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.DocNum= [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 200,18)];
            cell.DocNum.text =[NSString stringWithFormat:@"¥ %@ ", [price objectAtIndex:indexPath.row]];
            
            cell.DocNum.textColor=zhonghuiColor;
            cell.DocNum.font= [UIFont fontWithName:@"Arial" size:18];
            [cell.contentView addSubview: cell.DocNum];
            
            
            NSString*GoldPrice=[NSString stringWithFormat:@"%@",[useLimitAmt objectAtIndex:indexPath.row]];
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            [numberFormatter setPositiveFormat:@"###,##0;"];
            NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
            
            UILabel *useLimitAmt1=[[UILabel alloc]initWithFrame:CGRectMake(15,  55, 150, 14)];
            
            useLimitAmt1.text=[NSString stringWithFormat:@"满 ¥ %@ 使用",formattedNumberString];
            
            useLimitAmt1.textColor=zhonghuiColor;
            useLimitAmt1.font=[UIFont systemFontOfSize:14];
            
            [cell.contentView addSubview:useLimitAmt1];
            
            UILabel *Date =[[UILabel alloc]initWithFrame:CGRectMake(15,  75, 320, 14)];
            
            
            
            
            NSRange range={0,10};
            NSString* string =[[beginDate objectAtIndex:indexPath.row] substringWithRange:range];
            NSRange range1={0,10};
            NSString* string1 =[[endDate objectAtIndex:indexPath.row] substringWithRange:range1];
            Date .text=[NSString stringWithFormat:@"有效期 %@ ~ %@",string,string1];
            
            
            Date .textColor=zhonghuiColor;
            Date .font=[UIFont systemFontOfSize:14];
            
            [cell.contentView addSubview:Date ];
            
            
            
            UILabel *isUsed1 =[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-180,  20, 160, 14)];
            
            isUsed1 .text=[NSString stringWithFormat:@"%@",@"已使用"];
            isUsed1.textAlignment=NSTextAlignmentRight;
            isUsed1 .textColor=zhonghuiColor;
            isUsed1 .font=[UIFont systemFontOfSize:14];
            
            [cell.contentView addSubview:isUsed1 ];
            
            //            NSRange range2={0,10};
            //            NSString* string2 =[[useTime objectAtIndex:indexPath.row] substringWithRange:range2];
            
            
            UILabel *time =[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-180,  75, 160, 14)];
            time .text=[NSString stringWithFormat:@"%@",[useTime objectAtIndex:indexPath.row]];
            time.textAlignment=NSTextAlignmentRight;
            time .textColor=shenhuicolor;
            time .font=[UIFont systemFontOfSize:14];
            [cell.contentView addSubview:time ];
            
        }else{
            cell = [[orderListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.DocNum= [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 200,18)];
            cell.DocNum.text =[NSString stringWithFormat:@"¥ %@ ", [price objectAtIndex:indexPath.row]];
            
            cell.DocNum.textColor=[UIColor blackColor];
            
            cell.DocNum.font= [UIFont fontWithName:@"Arial" size:18];
            [cell.contentView addSubview: cell.DocNum];
            UILabel *useLimitAmt1=[[UILabel alloc]initWithFrame:CGRectMake(15,  55, 150, 14)];
            
            useLimitAmt1.text=[NSString stringWithFormat:@"满 ¥ %@ 使用",[useLimitAmt objectAtIndex:indexPath.row]];
            
            useLimitAmt1.textColor=shenhuicolor;
            useLimitAmt1.font=[UIFont systemFontOfSize:14];
            
            [cell.contentView addSubview:useLimitAmt1];
            
            UILabel *Date =[[UILabel alloc]initWithFrame:CGRectMake(15,  75, 320, 14)];
            
            
            
            NSRange range={0,10};
            NSString* string =[[beginDate objectAtIndex:indexPath.row] substringWithRange:range];
            NSRange range1={0,10};
            NSString* string1 =[[endDate objectAtIndex:indexPath.row] substringWithRange:range1];
            Date .text=[NSString stringWithFormat:@"有效期 %@ ~ %@",string,string1];
            
            Date .textColor=shenhuicolor;
            Date .font=[UIFont systemFontOfSize:14];
            
            [cell.contentView addSubview:Date ];
            
            
            
            UILabel *isUsed1 =[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-180,  20, 160, 14)];
            
            isUsed1 .text=[NSString stringWithFormat:@"%@",@"未使用"];
            isUsed1.textAlignment=NSTextAlignmentRight;
            isUsed1 .textColor=luseColor;
            isUsed1 .font=[UIFont systemFontOfSize:14];
            [cell.contentView addSubview:isUsed1 ];
            
            
            
            
            
        }
        
        
        
        
        if (price.count !=indexPath.row+1) {
            UILabel *lineLabel21=[[UILabel alloc] initWithFrame:CGRectMake(15, 105-0.35, ScreenWidth-30, 0.35)];
            lineLabel21.backgroundColor=[UIColor groupTableViewBackgroundColor];;
            [cell.contentView addSubview:lineLabel21];
            
        }
        
        if (price.count ==indexPath.row+1) {
            UILabel *lineLabel21=[[UILabel alloc] initWithFrame:CGRectMake(0, 105-0.35, ScreenWidth, 0.35)];
            lineLabel21.backgroundColor=[UIColor groupTableViewBackgroundColor];
            [cell.contentView addSubview:lineLabel21];
            
        }
        
        
        
    }
    
    
       return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 105;
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    if (tableView == table_list1) {
        
        //    commdity.actTypeCode=couponId[indexPath.row];
        //
        //    BusinessCouponDetailViewController*vc1=[[BusinessCouponDetailViewController alloc] init];
        //
        //    [self.navigationController pushViewController:vc1 animated:YES];
        //
        
    }
    }





- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
